/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import biz.teachersbiz;
import entity.Teachers;
import entityBean.TeachersFacade;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author ADMIN
 */
@ManagedBean
@RequestScoped
public class TeachersBean {

    @EJB
    private TeachersFacade teachersFacade;
    private Teachers teachers = new Teachers();
    //doi tuong luu du lieu gan vao form edit
    private Teachers teachers2 = new Teachers();
    private String id;
    teachersbiz teachersbiz = new teachersbiz();
    List<Teachers> list = new ArrayList<Teachers>();

    /** Creates a new instance of TeachersBean */
    public TeachersBean() {
        //get id querystring
        loadEdit();
        list = teachersbiz.listTeachers();
    }

    public void loadEdit() {
        try {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            id = request.getParameter("id");
            if (id != "") {
                teachers2 = teachersbiz.findTeachersById(Integer.parseInt(id));
            }
        } catch (Exception e) {
        }
    }

    public String insert() {
        teachersFacade.create(teachers);
        list = teachersbiz.listTeachers();
        return "listteachers";
    }

    public String delete(Teachers teachers) {
        teachersFacade.remove(teachers);
        list = teachersbiz.listTeachers();
        return "listteachers";
    }

    public String edit() {
        try {
            Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
            int id = Integer.parseInt(params.get("id"));
            teachersFacade.update(teachers2.getTname(), teachers2.getTpass(), teachers2.getTfullname(), id);
            list = teachersbiz.listTeachers();
        } catch (Exception e) {
        }

        return "listteachers";
    }

    public Teachers getTeachers() {
        return teachers;
    }

    public void setTeachers(Teachers teachers) {
        this.teachers = teachers;
    }

    public List<Teachers> getList() {
        return list;
    }

    public void setList(List<Teachers> list) {
        this.list = list;
    }

    public Teachers getTeachers2() {
        return teachers2;
    }

    public void setTeachers2(Teachers teachers2) {
        this.teachers2 = teachers2;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
